﻿using System.Collections.Generic;
using System;
using UnityEngine;

[ManagerName("UI管理器")]
public class UIManager : BaseManager, IUIManager
{
    private readonly List<IUIView> _views = new List<IUIView>();
    private readonly Dictionary<UILayer, List<IUIView>> _layerViews = new Dictionary<UILayer, List<IUIView>>();
    private readonly UIRoot _uiRoot;

    public UIManager(UIRoot uiRoot)
    {
        _uiRoot = uiRoot;

        foreach (UILayer layer in Enum.GetValues(typeof(UILayer)))
        {
            _layerViews[layer] = new List<IUIView>();
        }
    }

    public void RegisterView(IUIView view, UILayer layer)
    {
        if (view != null && !_views.Contains(view))
        {
            _views.Add(view);
            _layerViews[layer].Add(view);
        }
    }

    public void UnregisterView(IUIView view, UILayer layer)
    {
        if (view != null)
        {
            _views.Remove(view);
            _layerViews[layer].Remove(view);
        }
    }

    public void ShowView(IUIView view, UILayer layer)
    {
        if (view == null) return;
        if (_uiRoot == null)
        {
            Debug.LogWarning("UIRoot 未设置！");
            return;
        }

        var parent = _uiRoot.GetLayer(layer);
        if (parent == null)
        {
            Debug.LogWarning($"UIRoot 未找到层级: {layer}");
            return;
        }

        HideLayerViews(layer);

        var viewBase = view as UIViewBase;
        if (viewBase != null)
        {
            viewBase.transform.SetParent(parent, false);
            viewBase.gameObject.SetActive(true);
        }

        RegisterView(view, layer);
    }

    public TView ShowView<TView>(UILayer layer)
    where TView : UIViewBase, new()
    {
        // 1. 通过特性获取 ViewModel 类型
        var attr = typeof(TView).GetCustomAttributes(typeof(BindViewModelAttribute), false);
        ViewModelBase viewModel = null;
        if (attr.Length > 0)
        {
            var vmType = ((BindViewModelAttribute)attr[0]).ViewModelType;
            viewModel = Activator.CreateInstance(vmType) as ViewModelBase;
        }

        // 2. 创建 View
        var view = new TView();

        // 3. 绑定 ViewModel
        if (viewModel != null)
            view.BindViewModel(viewModel);

        // 4. 显示 View
        ShowView(view, layer);

        return view;
    }

    public void CloseView<T>() where T : IUIView
    {
        var view = GetView<T>();
        if (view != null)
        {
            var viewBase = view as UIViewBase;
            if (viewBase != null)
            {
                viewBase.gameObject.SetActive(false);
            }
            UnregisterView(view, GetViewLayer(view));
        }
    }

    public T GetView<T>() where T : IUIView
    {
        foreach (var view in _views)
        {
            if (view is T typedView)
            {
                return typedView;
            }
        }
        return default;
    }

    private UILayer GetViewLayer(IUIView view)
    {
        foreach (var kvp in _layerViews)
        {
            if (kvp.Value.Contains(view))
            {
                return kvp.Key;
            }
        }
        return UILayer.Main;
    }

    private void HideLayerViews(UILayer layer)
    {
        if (_layerViews.TryGetValue(layer, out var views))
        {
            foreach (var view in views)
            {
                var viewBase = view as UIViewBase;
                if (viewBase != null)
                {
                    viewBase.gameObject.SetActive(false);
                }
            }
        }
    }
}